package com.swf.seed.beanwraper;

import java.beans.PropertyDescriptor;
import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

public class TestCircle {
	public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException {
		Circle circle = new Circle(1,2,"bbabb","bab");
	/*	testField(circle);
		changeValue(circle);
		testField(circle);*/
		//testMethod(circle);
		testPd(circle);
		File file = new File("");
		System.out.println(file.getClass()==File.class);
	}
	
	public static void testPd(Circle circle){
		BeanWrapper beanWrapper = new BeanWrapperImpl(circle);
		PropertyDescriptor[] descriptors = beanWrapper.getPropertyDescriptors();
		for (PropertyDescriptor pd:descriptors){
			if (beanWrapper.isReadableProperty(pd.getName())){
				System.out.println("Readable:"+pd.getName());
			}
			if (beanWrapper.isWritableProperty(pd.getName())){
				System.out.println("Writable:"+pd.getName());
			}
		}
	}
	
	public static void changeValue(Circle obj) throws IllegalArgumentException, IllegalAccessException{
		//得到某个类的字节码在得到这个类中的成员变量
		Field[] fields=obj.getClass().getDeclaredFields();
		//遍历成员变量
		for(Field field:fields){
		//比较字节码用==，因为字节码只有一份
		if(field.getType()==String.class){
			field.setAccessible(true);
		//如是字符串字节码，则获取相应字符串
		String oldValue=(String)field.get(obj);

		String newValue=oldValue.replace("b","a");
		//将相应字段设置为改变后的值
		field.set(obj, newValue);
		}
		}
	}
	
	public static void testField(Circle circle) throws IllegalArgumentException, IllegalAccessException{
		Field[] fields = circle.getClass().getDeclaredFields();
		for (Field field:fields){
			field.setAccessible(true);
			System.out.println(field.getName()+":"+field.getDeclaringClass().getSimpleName()+":"+field.getType().getCanonicalName()+":"+Modifier.toString(field.getModifiers())+":"+field.get(circle));
		}
		System.out.println("--------------------------------------------------------------");
	}
	public static void testMethod(Circle circle) throws IllegalArgumentException, IllegalAccessException{
		Method[] methods = circle.getClass().getDeclaredMethods();
		for (Method method:methods){
			//method.setAccessible(true);
			print(method.getName(),Modifier.toString(method.getModifiers()),method.getReturnType());
			for (Class c:method.getParameterTypes()){
				System.out.println("ParameterTypes:"+c.getSimpleName());
			}
		}
		System.out.println("--------------------------------------------------------------");
	}
	
	public static void print(Object...  objs ){
		StringBuilder sb = new StringBuilder();
		for (Object o:objs){
			sb.append(":"+""+o.toString());
		}
		System.out.println(sb.substring(1));
	}
}
